package com.yrh.controller;

import com.yrh.bean.TUser;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 基础Servlet
 * 包含请求映射方法
 * 转发、重定向、返回Json等功能
 * @author Administrator
 *
 */
public class BaseServlet extends HttpServlet {

    private static final String SUFFIX = ".jsp";
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    /**
     * 转发跳转页面
     * @param request
     * @param response
     * @param url
     * @throws ServletException
     * @throws IOException
     */
    public void response(HttpServletRequest request, HttpServletResponse response, String url) {
        try {
            String toUrl = url + SUFFIX;
            request.getRequestDispatcher(toUrl).forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 重写向页面
     * @param response
     * @param url
     * @throws IOException
     */
    public void responseRedirect(HttpServletResponse response, String url) throws IOException {
        response.sendRedirect(url);
    }

    /**
     * 返回json
     * @param response
     * @param context
     * @throws IOException
     */
    public void responseJson(HttpServletResponse response, String context) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter() ;
        out.write(context);
        out.close();
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取请求的URI地址信息
        String url = request.getRequestURI();
        // 截取其中的方法名
        String methodName = url.substring(url.lastIndexOf("/")+1, url.length());
        TUser user = (TUser) request.getSession().getAttribute("user");
        //用户不能进入的页面
        List<String> list=new ArrayList<String>(Arrays.asList("allTUser","gotoCategory","gotoProduct"));
        if (user!=null&&"U".equals(user.getUserRole())&&list.contains(methodName)){
            response.setCharacterEncoding("utf-8");
            response.sendRedirect(request.getContextPath()+"/main.jsp");
            return;
        }
        //没有登录
        //不能进入的页面
        List<String> lists=new ArrayList<String>(Arrays.asList("allTUser","gotoCategory","gotoProduct","allAddress","gotoOrder","user_info.jsp"));
        if (user==null&&lists.contains(methodName)){
            response.sendRedirect(request.getContextPath()+"/main.jsp");
            return;
        }
        Method method = null;
        try {
            // 使用反射机制获取在本类中声明了的方法
            method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
        } catch (Exception e) {
            e.printStackTrace();
         /*   response(request, response, "404");*/
        }
        try {
            // 执行方法
            method.invoke(this, request, response);
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            e.printStackTrace();
         /*   response(request, response, "500");*/
        }
    }

}
